package com.hyfd.deal.Flow;

import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.GenerateData;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.common.utils.ToolDateTime;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.deal.BaseDeal;

/**
 * 赛越单号码流量充值接口
 *
 * @author Rocky
 *
 */
public class SaiYueFlowDeal implements BaseDeal
{
    Logger log = Logger.getLogger(getClass());

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deal(Map<String, Object> order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = -1;

        try
        {
            // 手机号
            String mobile = (String)order.get("phone");
            // 平台订单号
            String curids = ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS") + mobile + GenerateData.getIntData(9, 4);
            map.put("orderId", curids);
            // 账号
            Map<String, Object> channel = (Map<String, Object>)order.get("channel");// 获取通道参数
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String defaultParameter = (String)channel.get("default_parameter");// 默认参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String confId = paramMap.get("confId");//  户名
            String apiKey = paramMap.get("apiKey");// 平台的appKey
            String callback = paramMap.get("callback");// 平台的appKey
            // 0 漫游流量 1非漫游流量，不带改参数时默认为0
            String nation = paramMap.get("nation");
            // 套餐
            String spec = new Double(order.get("value") + "").intValue() + "";
            // 计算签名
            long t = new Date().getTime()/1000;
            String md5Str = apiKey + confId + mobile + t;
            String sign = MD5.ToMD5(md5Str);
            // 组装参数
            String param ="confId=" + confId + "&mobile=" + mobile + "&flow=" + spec + "&callback="
                            + callback +"&t="+t+ "&sign=" + sign + "&seqNo=" + curids + "&nation=" + nation;
            // get请求
            String result = ToolHttp.get(false, linkUrl+"?"+param);
            // 判断是否成功
            if (result != null)
            {
                JSONObject jObject = JSONObject.parseObject(result);
                String allocateId = jObject.getString("allocateId");
                map.put("providerOrderId", allocateId);
                String retCode = jObject.getString("retCode");
                String retMsg = jObject.getString("retMsg");
                map.put("resultCode", retCode + ":" + retMsg);
                    // code=0为成功
                    if ("0".equals(retCode))
                    {
                        flag = 1;
                    }
                    else
                    {
                        flag = 0;
                    }
            }
        }
        catch (ConnectTimeoutException | SocketTimeoutException e)
        {// 请求、响应超时
            flag = -1;
        }
        catch (Exception e)
        {
            log.error("saiyue流量出错" + e + MapUtils.toString(order));
        }
        map.put("status", flag);
        return map;
    }
}
